# -*- coding: utf-8 -*-

from flask.ext.restful import Resource, abort
from PalmWebsite.model import Site
from PalmWebsite.api import parser, request_cache
from PalmWebsite.extensions import db
from spider import seo, beian
import utils
from datetime import datetime, date

def domain_exist(domain):
    site = Site.query.filter_by(domain=utils.get_host(domain)).first()
    if site:
        return True

    return False


class SiteApi(Resource):

    def put(self):
        args = parser.parse_args()
        if args.get("domain"):
            domain = utils.get_host(args["domain"])

            if domain_exist(domain):
                abort(400, success=False, err_code=30002)

            site = Site(domain)
            db.session.add(site)
            db.session.commit()
            return {
                    "success": True,
                    "data": {
                        "id": site.id,
                        "domain": domain,
                        "date": utils.date2string(site.date)
                    }
                }
        abort(400, success=False, err_code=10000)


    def get(self):
        args = parser.parse_args()

        if args.get("domain"):
            if domain_exist(args.get("domain")):
                return {
                        "success": True,
                        "data": ""
                    }
            else:
                abort(404, success=False, err_code=30001)

        abort(400, success=False, err_code=10000)


class SiteInfo(Resource):

    def get(self):
        """keywords, title, description"""
        args = parser.parse_args()
        domain = args.get("domain")

        if not domain:
            abort(400, success=False, err_code=10000)

        site = Site.query.filter_by(domain=utils.get_host(domain)).first()

        if not site:
            site_info = seo.SEO(domain)
            site_info.parser()
            return {
                    "success": True,
                    "data": {
                        "domain": domain,
                        "title": site_info.title,
                        "keywords": site_info.keywords,
                        "description": site_info.description
                    }
                }

        if args.get("refresh"):
            site_info = seo.SEO(site.domain)
            site_info.parser()
            site.title = site_info.title
            site.keywords = site_info.keywords
            site.description = site_info.description
            site.last_update_date = date.today()
            db.session.commit()

        return {
                "success": True,
                "data": {
                    "id": site.id,
                    "domain": site.domain,
                    "title": site.title,
                    "keywords": site.keywords,
                    "description": site.description,
                    "last_update_date": utils.date2string(site.last_update_date)
                }
            }


class SiteBeian(Resource):
    def get(self):
        args = parser.parse_args()
        domain = args.get("domain")

        if not domain:
            abort(400, success=False, err_code=10000)

        site = Site.query.filter_by(domain=utils.get_host(domain)).first()

        if not site:
            site_beian = beian.get_beian_info(domain)
            return {
                    "success": True,
                    "data": site_beian
                }

        if args.get("refresh"):
            site_beian = beian.get_beian_info(site.domain)
            if site_beian:
                site.beian = site_beian["icpno"]
                site.beian_date = utils.beian_date(site_beian["update"])
                site.organizers = site_beian["organizers"]
                db.session.commit()
        return {
                "success": True,
                "data": {
                    "id": site.id,
                    "domain": site.domain,
                    "icpno": site.beian,
                    "organizers": site.organizers,
                    "beian_date": site.beian_date.isoformat() if site.beian_date else None
                }
            }
